package com.cg.controller;

import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.Source;
import com.cg.service.SourceService;
import com.cg.vo.MenuAllVo;
import com.cg.vo.SourceForRoleVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Source)表控制层
 *
 * @author chengong
 * @since 2023-12-04 09:24:43
 */
@RestController
@RequestMapping("source")
public class SourceController {
    /**
     * 服务对象
     */
    @Resource
    private SourceService sourceService;

    @PostMapping("/createSource")
    Result createSource(@RequestBody Source source){
        sourceService.createSource(source);
        return ResultUtils.returnSuccess();
    }

    /**
     * 通过角色id获取角色信息
     * @param roleId
     * @return
     */
    @PostMapping("/querySourceBySourceId")
    public Result<SourceForRoleVo> querySourceBySourceId(@RequestParam("roleId") Integer roleId){
        SourceForRoleVo sourceForRoleVo = sourceService.querySourceBySourceId(roleId);
        return ResultUtils.returnDataSuccess(sourceForRoleVo);
    }


    /**
     * 加载所有的以及二级菜单权限
     */
    @PostMapping("/queryMenuAll")
    public Result<List<MenuAllVo>> queryMenuAll(){
        List<MenuAllVo> menuAllVos = sourceService.queryMenuAll();
        return ResultUtils.returnDataSuccess(menuAllVos);
    }


    /**
     * 验证权限是否存在
     * @param url
     * @return
     */
    @GetMapping("/checkSourceUrl")
    public Result checkSourceUrl(@RequestParam("url") String url){
        Integer integer = sourceService.checkSourceUrl(url);
        if(integer >0){
            return ResultUtils.returnResult(ResultEnum.FAIL_SOURCE_CHECK);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 删除权限
     */
    @GetMapping("/removeSource")
    public Result removeSource(@RequestParam("sourceId") Integer sourceId){
        Integer integer = sourceService.removeSource(sourceId);
        if(integer == 0){
            return ResultUtils.returnResult(ResultEnum.FAIL_SOURCE_DELETE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 修改权限的名称
     * @param source
     * @return
     */
    @PostMapping("/updateSourceName")
    public Result updateSourceName(@RequestBody Source source){
        Integer integer = sourceService.updateSourceName(source);
        if(integer <=0){
            return ResultUtils.returnFail("权限名称修改失败");
        }
        return ResultUtils.returnSuccess();
    }

}

